//
//  YCServiceButton.m
//  YCSchool
//
//  Created by Yanglixia on 16/4/28.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//

#import "YCServiceButton.h"

@implementation YCServiceButton

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.titleLabel.font = [UIFont systemFontOfSize:13];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.imageView.contentMode = UIViewContentModeCenter;
        self.layer.cornerRadius = 3;
        self.layer.masksToBounds = YES;
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.titleLabel sizeToFit];
    
    CGSize strSize = [self.titleLabel.text yc_sizeWithFont:self.titleLabel.font MaxSize:CGSizeMake(MAXFLOAT, 0)];//size范围宽度不限制,求宽度
    CGFloat totalLen = strSize.width + 15 + 12;//12间距+图片15宽度+文字宽
    CGFloat edgeLen = (self.frame.size.width - totalLen) / 2.0;
    [self setImageEdgeInsets:UIEdgeInsetsMake(0, -edgeLen, 0, 0)];
    [self setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -edgeLen)];
    
    // 图片大小不一,设定frame
    CGFloat y = (self.frame.size.height - 15) / 2.0;
    CGRect frame = self.imageView.frame;
    frame.origin.y = y;
    frame.size = CGSizeMake(15, 15);
    self.imageView.frame = frame;
}

@end
